package com.blue.firecat.serivce.impl;

import com.blue.firecat.contants.Contants;
import com.blue.firecat.contants.Result;
import com.blue.firecat.dao.GameDao;
import com.blue.firecat.entity.Game;
import com.blue.firecat.entity.GameExample;
import com.blue.firecat.enums.ErrorEnum;
import com.blue.firecat.serivce.GameService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @描述 游戏的增删改查
 * @时间 2017/6/22 22:07
 * @作者 肖亦鸣
 */
@Service
public class GameServiceImpl implements GameService {
    @Autowired
    GameDao gameDao;

    @Override
    public Game selectByPrimaryKey(Integer id) {
        return gameDao.selectByPrimaryKey(id);
    }

    @Override
    //增加游戏之前先删除缓存
    public Result<List<Game>> insertSelective(Game game) {
        Contants.GAME_RESULT.getData().clear();
        Contants.TRADE_GAME_RESULT.getData().clear();
        Result<List<Game>> result = new Result<>();
        if (StringUtils.isNotBlank(game.getName())) {
            int i = gameDao.insertSelective(game);
            if (i == 0) {
                result.setCode(ErrorEnum.GAME_ADD_ERROR);
            }
            result.setMessage(ErrorEnum.GAME_ADD_SUCCESS.getValue());
        } else {
            result.setMessage(ErrorEnum.GAME_NAME_NOT_EMPTY.getValue());
        }
        result.setData(this.selectByExample(null).getData());
        return result;
    }

    @Override
    public Result<List<Game>> selectByExample(GameExample example) {
        //有缓存,则直接放回
        if (CollectionUtils.isNotEmpty(Contants.GAME_RESULT.getData())) {
            return Contants.GAME_RESULT;
        }
        //没有缓存,则查询数据库
        Result<List<Game>> result = Contants.GAME_RESULT;
        List<Game> games = gameDao.selectByExample(example);
        if (CollectionUtils.isEmpty(games)) {
            result.setCode(ErrorEnum.GAME_IS_EMPTY);
        }
        Contants.TRADE_GAME_RESULT.setData(gameDao.selectByExample(example));
        //加上计数器
        for (Game game : games) {
            game.setName(game.getName() + "(" + Contants.GAME_COUNT_MAP.get(game.getId()) + ")");
        }

        result.setData(games);
        return result;
    }

    @Override
    public int updateByPrimaryKeySelective(Game record) {
        //增加游戏之前先删除缓存
        Contants.GAME_RESULT.getData().clear();
        Contants.TRADE_GAME_RESULT.getData().clear();
        return gameDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public Result<Game> deleteByPrimaryKey(Integer id) {
        //增加游戏之前先删除缓存
        Contants.GAME_RESULT.getData().clear();
        Contants.TRADE_GAME_RESULT.getData().clear();
        Result<Game> result = new Result<>();
        int i = gameDao.deleteByPrimaryKey(id);
        if (i == 0) {
            result.setCode(ErrorEnum.GAME_DELETE_ERROR);
        }
        return result;
    }

    @Override
    public void countByExample() {
        GameExample example = new GameExample();

        long l = gameDao.countByExample(example);

    }
}
